<template>
  <div class="ladderPrice119">
    <TopBar @search='search' @changeSortTab='changeSortTab' :showBanner='showBanner' v-model="titleTabActive" :keyWord='from.keyWord' @changeTab="changeTab" :dataSourse='banner' />
    <!-- //商品列表 -->
    <LadderGoodsList :is119='is119' :isPreview='isPreview' @showCards='showCards' :dataSoures='ladderGoodsList' :finished='finished' :loading='loading' @load='findPageTieredPricingGoods(1)' />
    <!-- //弹窗详情 -->
    <DialogInfo :is119='is119' :everyDayInfo='everyDayInfo' :show='show' @close='close' type='阶梯价' />
    <div class='upgrade' @click='goTop' v-if="goTopShow">
      <img src="https://image.hezongyy.com/pcimage/119h5/goup.png" alt="">
    </div>
    <van-overlay :show="activeLoadingShow" @click="show = false">
      <div class="wrapper" @click.stop>
        <div class="activeLoading"><img :src="activeLoading" alt="" /></div>
      </div>
    </van-overlay>
  </div>
</template>
<script>
import { getRequest } from '@/api/indexGet'
import { postRequest } from '@/api/index'
import url from '@/axios/apiUrl'
import Xpng from '@/assets/image/X.png'
import TopBar from './topBar.vue'
import DialogInfo from '@/components/DialogInfo'
import activeLoading from '@/assets/image/activeLoading.gif'
import LadderGoodsList from '@/components/LadderGoodsList'
export default {
  components: {
    TopBar,
    DialogInfo,
    LadderGoodsList,
  },
  data() {
    return {
      // showColor: false,
      goTopShow: false,
      showBanner: true,
      showGoodsList: false,
      activeLoading,
      activeLoadingShow: false,
      Xpng, //图片关闭按钮
      loading: false, //加载
      finished: false, //加载完成
      from: {
        pageNumber: 1,
        pageSize: 40,
        keyWord: '', //搜索关键字
        searchType: 0, //搜索类型 0 查询全部  1 已加入购物车  2未选择购物车
        // goodsTypeList: [2, 3], //数组用goodsTypeList 字符串用goodsType
        // testFlag: true,
      },
      show: false, //弹窗开关
      everyDayInfo: {}, ///弹窗信息详情
      banner: [], // banner
      titleTabActive: 0, // 0 /全部  1 已加购 2未加购
      ladderGoodsList: [], //阶梯价列表
      // num: '', //阶梯价已加购数量信息
      is119: false,
      isPreview: false,
    }
  },
  created() {
    this.time119()
  },
  async mounted() {
    //阶梯价商品列表
    this.findPageTieredPricingGoods()
    // this.findPageNum()
    this.getBanner()
    let node = document.querySelector('.ladder_goods_area')
    node.onscroll = () => {
      if (
        node.scrollTop > 150 &&
        this.ladderGoodsList &&
        this.ladderGoodsList.length > 1
      ) {
        this.showBanner = false
      } else if (node.scrollTop == 0) {
        this.showBanner = true
        this.goTopShow = false
      }
      if (node.scrollTop > 600 && this.ladderGoodsList) {
        this.goTopShow = true
      }
    }
  },
  beforeDestroy() {
    clearInterval(this.timeName)
    clearInterval(this.curTime)
  },
  methods: {
    //活动banner阶梯价
    async getBanner() {
      let res = await postRequest(url.adPost, {
        pageName: 'tieredSpecialPerformance',
        clientType: 1,
      })
      if (res.data.code == '000000') {
        this.banner = res.data.content.tieredSpecialPerformanceBanner.items
      } else {
        this.$toast(res.data.message)
      }
    },
    //119时间
    async time119() {
      let res = await getRequest(url.time119)
      if (res.data.code == '000000') {
        this.is119 = res.data.content.is119
        this.isPreview = res.data.content.isPreview
        // this.startTime = res.data.content.start
        // this.nowTime = res.data.content.now
        // this.nowTime = 1634287318784
        // this.startTime = 1634287322784
      }
      // this.countDownInit()
      // this.getNowTime()
    },
    // // 获取当前时间
    // countDownInit() {
    //   clearInterval(this.curTime)
    //   this.curTime = setInterval(() => {
    //     this.nowTime = this.nowTime + 1000
    //   }, 1000)
    // },
    // // 计算时间
    // getNowTime() {
    //   this.timeName = setInterval(() => {
    //     if (this.nowTime >= this.startTime) {
    //       this.showColor = true
    //     }
    //     if (this.showColor) {
    //       this.from.pageNumber = 1
    //       this.findPageTieredPricingGoods()
    //       clearInterval(this.timeName)
    //       clearInterval(this.curTime)
    //     }
    //   }, 1000)
    // },
    changeSortTab(item) {
      this.from.keyWord = ''
      // this.from.goodsTypeList = item.value
      this.from.pageNumber = 1
      this.from.searchType = 0
      this.titleTabActive = 0
      this.findPageTieredPricingGoods()
      // this.findPageNum()
    },
    //阶梯价获取数量
    // async findPageNum() {
    //   let from = {
    //     pageNumber: 1,
    //     pageSize: 10,
    //     keyWord: this.from.keyWord, //搜索关键字
    //     searchType: 1, //搜索类型 0 查询全部  1 已加入购物车  2未选择购物车
    //     // goodsTypeList: this.from.goodsTypeList,
    //   }
    //   let res = await postRequest(url.findPageNum, from)
    //   if (res.data.code == '000000') {
    //     this.num = res.data.content
    //   } else if (res.data.message == '系统异常') {
    //     this.$toast('网络异常，请稍后再试')
    //   } else if (res.data.code != '000000') {
    //     this.$toast(res.data.message)
    //   }
    // },
    //集采标识显示
    getLabelImg(labelId) {
      if (labelId == 21)
        return 'https://image.hezongyy.com/pcimage/119h5/h5_jc.png'
      if (labelId == 8)
        return 'https://image.hezongyy.com/pcimage/119h5/h5_jm.png'
    },
    //阶梯价跳转原生详情页面
    goToDetail(item) {
      window.location.href =
        this.pathURL + 'nativeApp/goodsDetails?id=' + item.id
    },
    //tab筛选
    changeTab(type) {
      this.from.pageNumber = 1
      if (type == 0) {
        this.from.searchType = 0
      } else if (type == 1) {
        this.from.searchType = 1
      } else if (type == 2) {
        this.from.searchType = 2
      }
      this.findPageTieredPricingGoods()
      //切换回到顶部
      this.goTop()
    },
    //回到顶部
    goTop() {
      let node = document.querySelector('.ladder_goods_area')
      if (node) {
        node.scrollTop = 0
      } else {
        node.scrollTop = 0
      }
    },
    //关闭弹窗
    close() {
      this.show = false
      // this.findPageNum()
      // this.from.pageNumber = 1
      // this.findPageTieredPricingGoods()
    },
    //加购弹窗
    showCards(item) {
      this.show = true
      this.everyDayInfo = item
    },
    //商品列表
    async findPageTieredPricingGoods() {
      this.loading = true
      if (this.from.pageNumber == 1) {
        this.activeLoadingShow = true
      }
      let res = await postRequest(url.findPageTieredPricingGoods, this.from)
      if (res.data.code == '000000') {
        this.activeLoadingShow = false
        if (!res.data.content) {
          this.finished = true
        }
        if (this.from.pageNumber == 1) {
          this.ladderGoodsList = []
        }
        if (res.data.content.list == []) {
          this.finished = true
          return
        }
        this.loading = false
        this.from.pageNumber++
        this.ladderGoodsList = this.ladderGoodsList.concat(
          res.data.content.list
        )
        if (res.data.content && res.data.content.list.length == 0) {
          // 数据全部加载完成
          this.finished = true
        } else {
          this.finished = false
        }
        if (this.ladderGoodsList) {
          this.ladderGoodsList.map((obj) => {
            this.$set(obj, 'initializeMinimum', 1)
            this.$set(obj, 'maximum', obj.maxBuyNumber)
            this.goodsAdd.pageInit(obj, this)
          })
        }
      } else {
        this.finished = true
        this.activeLoadingShow = false
        this.$toast(res.data.message)
      }
    },
    //商品列表搜索
    async search(value) {
      console.log(value,'---value---')
      this.from.keyWord = value
      this.from.pageNumber = 1
      this.findPageTieredPricingGoods()
      // this.findPageNum()
      let node = document.querySelector('.ladder_goods_area')
      node.scrollTop = 0
    },
  },
  beforeDestroy() {},
}
</script>
<style lang="less" src="./index.less" scoped></style>
